package com.zhenhao.xsq.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;

import javax.inject.Singleton;

/**
 * 提示工具类
 *
 * @author lijing
 */
@Singleton
public class ToastUtil {

    private static Toast longToast;
    private static Toast shortToast;
    private static Toast toast;
    private static Runnable run = new Runnable() {
        public void run() {
            toast.cancel();
        }
    };
    private Toast customDurationToast;

    public static Toast displayShortToast(final String text) {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                if (shortToast != null) {
                    shortToast.cancel();
                }
                shortToast = Toast.makeText(AppConfig.getInstance(), text, Toast.LENGTH_SHORT);
                shortToast.setGravity(Gravity.CENTER, 0, 0);
                shortToast.show();
                Looper.loop();
            }
        }.start();
        return shortToast;
    }

    public static void showShort(String info) {
        if (toast == null) {
            toast = Toast.makeText(AppConfig.getInstance(), info, Toast.LENGTH_SHORT);
        } else {
            toast.setText(info);
        }
        toast.show();
    }

    public static void show(Context context, String info) {
        if (toast != null) {
            toast.cancel();
            toast = Toast.makeText(context, info, Toast.LENGTH_SHORT);
        } else {
            toast = Toast.makeText(context, info, Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    public static void showLong(String info) {
        if (toast == null) toast = Toast.makeText(AppConfig.getInstance(), info, Toast.LENGTH_LONG);
        else toast.setText(info);
        toast.show();
    }

    public static void showCenterShort(String info) {
        if (TextUtils.isEmpty(info)) return;
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
////        ToastUtils.setBgColor(Color.parseColor("#CC000000"));
        ToastUtils.setMsgTextSize(15);
        ToastUtils.setBgResource(R.drawable.shape_bg_black_5);
        ToastUtils.setMsgColor(Color.parseColor("#ffffff"));
        ToastUtils.showShort(info);
//        if (toast == null) {
//            toast = Toast.makeText(AppConfig.getInstance(), info, Toast.LENGTH_SHORT);
//        } else {
//            toast.setText(info);
//        }
//        toast.setGravity(Gravity.CENTER, 0, 0);
//        toast.show();
//        showLong(info);
    }

    public static void showCenterLong(String info) {
        if (TextUtils.isEmpty(info)) return;
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
//        ToastUtils.setBgColor(Color.parseColor("#CC000000"));
        ToastUtils.setMsgTextSize(15);
        ToastUtils.setBgResource(R.drawable.shape_bg_black_5);
        ToastUtils.setMsgColor(Color.parseColor("#ffffff"));
        ToastUtils.showLong(info);
//        if (toast == null) {
//            toast = Toast.makeText(AppConfig.getInstance(), info, Toast.LENGTH_LONG);
//        } else {
//            toast.setText(info);
//        }
//        toast.setGravity(Gravity.CENTER, 0, 0);
//        toast.show();
    }

    public Toast displayLongToast(final String text) {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                if (longToast != null) {
                    longToast.cancel();
                }
                longToast = Toast.makeText(AppConfig.getInstance(), text, Toast.LENGTH_LONG);
                longToast.show();
                Looper.loop();
            }
        }.start();
        return longToast;
    }

    public Toast displayCustomDurationToast(final String text, final int duration) {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                if (customDurationToast != null) {
                    customDurationToast.cancel();
                }
                customDurationToast = Toast.makeText(AppConfig.getInstance(), text, duration);
                customDurationToast.show();
                Looper.loop();
            }
        }.start();
        return customDurationToast;
    }
//    public static void
}
